package com.wy2cloud.sample.mongodemo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wy2cloud.sample.mongodemo.entity.SystemUser;
import com.wy2cloud.sample.mongodemo.service.UserService;

import io.swagger.annotations.ApiOperation;

@RestController
public class HomeController {
	
	@Autowired
	private UserService userService;

	@ApiOperation(value="列表信息", notes="列表信息")
	@GetMapping({"/",""})
	public Page<SystemUser> index(@RequestParam(name="pageIndex",defaultValue="0")Integer pageIndex){
		PageRequest page=new PageRequest(pageIndex, 3,Direction.DESC,"userId");
		return userService.findAll(page);
	}
	
	@ApiOperation(value="读取信息", notes="读取信息")
	@GetMapping({"/findOne/{userId}"})
	public SystemUser findOne(@PathVariable("userId")Integer userId){
		return userService.findOne(userId);
	}
	
	@ApiOperation(value="添加信息", notes="添加信息")
	@PostMapping({"/add"})
	public String add(@RequestBody SystemUser user){
		userService.add(user);		
		return "success";
	}
}
